<include file="./VMSTpl/Admin/header.html" />
<div class="container">
  <h2>{$title}</h2>
  <div class="col-md-12">
    <a id="unit" type="button" class="btn btn-default {$active[0]}" href="?ch=unit" >修改单位信息</a>
    <a id="major" type="button" class="btn btn-default {$active[1]}" href="?ch=major">修改专业信息</a>
    <a id="photos_position" type="button" class="btn btn-default {$active[2]}" href="?ch=photos_position">修改照片部位信息</a>
    <a id="wtime" type="button" class="btn btn-default {$active[3]}" href="?ch=wtime">修改工作时机</a>
  </div>
  <div class="col-md-8" style="margin-top:20px;{$style}">
    <table class="table table-striped col-md-12 main table-hover">
      <thead>
        <tr>
          <td class="col-md-4">id</td>
          <td class="col-md-4">内容</td>
          <td class="col-md-4">操作</td>
        </tr>
      </thead>
      <tbody>
      <volist name="data" id="vo">
        <tr for="{$vo['id']}">
          <td>{$vo['id']}</td>
          <td><p class="changable" data="{$vo['data']}" for="{$vo['id']}">{$vo['name']}</p></td>
          <td><a hret="#" class="btn btn-warning change">修改</a>&nbsp;&nbsp;&nbsp;<a class="del btn btn-danger" hret="#" data="{$vo['data']}" for="{$vo['id']}">删除</a></td>
        </tr>
      </volist>
      </tbody>
    </table>
    <button class="btn btn-primary col-md-offset-3" data-toggle="modal" data-target="#myModal">增加记录</button>
    <a  href="__ROOT__/Admin/Index" class="btn btn-default">返回</a>
  </div>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">添加记录</h4>
      </div>
      <div class="modal-body">
      	<div class="form-group">
      		<input type="text" class="form-control" id="Tid" placeholder="序号（可以不填）" />
         </div>
         <div class="form-group">
        	<input type="text" class="form-control" id="addable" data="{$vo['data']}" for="{$vo['id']}" placeholder="内容" />
         </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="button" class="btn btn-primary add">保存</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<include file="./VMSTpl/Admin/tailer.html" /> 
<script>
$("document").ready(function(){
	$(".changable").click(function(){
		var td = $(this);
        var txt = td.text();
		var data = $(this).attr('data');
		var id = $(this).attr('for');
        var input = $("<input type='text'value='" + txt + "'/>");
        td.html(input);
        input.click(function() {
            return false;
        });
        //获取焦点 
        input.trigger("focus");
        //文本框失去焦点后提交内容，重新变为文本 
        input.blur(function() {
            var newtxt = $(this).val();
            //判断文本有没有修改 
            if (newtxt != txt) {
                td.html(newtxt);

				var url = "__ROOT__/Admin/dataChange?datas=" + newtxt + "&id=" + id + "&data=" + data; 
				 
				$.get(url, function(data) { 
				if(data=="2") { 
					alert("该类别已存在！"); 
					td.html(txt); 
					return; 
				} 
				if(data=="3") {
					alert("保存失败！"); 
					td.html(txt); 
					return;
				}
				if(data=="1") {
					alert("非法操作！"); 
					td.html(txt); 
					return;
				}
					alert("修改成功！"); 
					td.html(newtxt); 
				}); 
			
            } else {
                td.html(newtxt);
            }
        });
	})	
	
	$('.change').click(function(){
		$(this).parent().prev().find(".changable").click();
	})
	
	$('.del').click(function(){
		var data = $(this).attr('data');
		var id = $(this).attr('for');
		if(confirm("确定要删除需要为"+id+"的记录？")){
			var url = "__ROOT__/Admin/dataDel?id=" + id + "&data=" + data; 
			$.get(url, function(data){
				if(data==1){
					alert("删除失败！"); 
					return;
				}
				alert("删除成功！");
				$("tr[for="+id+"]").remove();
			})
		}
	})
	
	$('.add').click(function(){
		var data = $('#addable').attr('data');
		var datas = $('#addable').val();
		var id = $('#Tid').val();
		
		var url = "__ROOT__/Admin/dataAdd?datas=" + datas + "&data=" + data + "&id=" + id; 
		$.get(url, function(data) { 
			if(data==4){
				alert("id="+id+"的记录已存在！");
				return;
			}
			if(data==3){
				alert("该记录已存在！");
				return;
			}
			if(data==2){
				alert("添加失败！");
				return;
			}
			alert("添加成功！");
			history.go(0) ;
		})
	})
})
</script>